/**
 * 定义数采仪管理模块
 */
(function(window, angular, undefined){
	var assetsRegisterManager = angular.module('water.apps.common.assetsRegister.assetsRegisterManager', ['water.entity.assetsRegister']);
	
	assetsRegisterManager.controller('commonDataAssetsRegisterManagerController',function($rootScope,$scope,$assetsRegisterService,$controller,$stateParams){
		/**
		 * 是否退役条件选项
		 * 默认选项全部
		 */
		$scope.serveOptionModel="all";

		/**
		 * 
		 */
		$scope.serveOption=[{v:"全部",k:"all"},{v:"退役",k:"1"},{v:"在役",k:"0"}];
		
		/**
		 * 新增对话框
		 */
		$scope.createDialog;
		/**
		 * 编辑对话框对象
		 */
		$scope.editDialog;
		/**
		 * 监听站点列表点击事件
		 */
		$scope.dataPramChangedBroadCast=$scope.$on('dataPramChanged', function(event, data) {
			$scope.stationModel = data;
			$scope._getList();
	    });
		/**
		 * 服役情况选项
		 */
		$scope.onServeOptionClick=function(){
			$scope._getList();
		}
		/**
		 * 新增按钮点击事件
		 */
		$scope.onCreateClick = function(){	
			$scope.assetsRegister={};
			$scope.createDialog.open();
		}
		/**
		 * 关闭新增窗口事件
		 */
		$scope.onCreateCloseClick = function(){	
			$scope.assetsRegister={};
			$scope.createDialog.close();
		}
		/**
		 * 提交新建按钮点击事件
		 */
		$scope.onSubmitCreateClick = function(data){
			data.STATION_CODE=$scope.stationModel.STATION_ID;
			data.WWTP_ID=$scope.stationModel.MN;
			if($scope.saveVerify(data)==false){
				return;
			}
			$assetsRegisterService.createLifecycleModel(data).then(function(){//资产管理生命周期数据新建
				$assetsRegisterService.createModel(data).then(function(){
					$.messager.alert("保存成功","保存成功",'info');
					$scope.createDialog.close();
					$scope._getList($scope.stationModel.STATION_ID);
				});
			});
		}
		/**
		 * 编辑按钮点击事件
		 */
		$scope.onEditClick = function(curRow){	
			$scope.Lifecycle={};
			$scope.Lifecycle = angular.copy(curRow);
			$scope.editDialog.open();
		}
		/**
		 * 关闭编辑窗口事件
		 */
		$scope.onEditCloseClick = function(){	
			$scope.Lifecycle={};
			$scope.editDialog.close();
		}
		/**
		 * 提交编辑按钮点击事件
		 */
		$scope.onSubmitEditClick = function(data){
			if($scope.saveVerify(data)==false){
				return;
			}
			if(data.GUID){
				$assetsRegisterService.updateLifecycleModel(data).then(function(){
					$.messager.alert("保存成功","保存成功",'info');				
					$scope.editDialog.close();
					$scope._getList($scope.stationModel.STATION_ID);
				});
			}
			else{
				delete data.GUID;
				$assetsRegisterService.createLifecycleModel(data).then(function(){
					$.messager.alert("保存成功","保存成功",'info');				
					$scope.editDialog.close();
					$scope._getList($scope.stationModel.STATION_ID);
				});
			}
			
		}
		/**
		 * 删除仪器
		 */
		$scope.onDeleteClick = function(data){
			$.messager.confirm("确认","确定要删除这条记录吗？",function(r){
				if(r){
					$assetsRegisterService.deleteLifecycleModel(data.GUID).then(function(){
						$assetsRegisterService.deleteModel(data.DEVICE_NUM).then(function(){
							$.messager.alert("删除成功","删除成功",'info');
							$scope._getList($scope.stationModel.STATION_ID);
						});
					});
				}
			});
				
		}
		//保存信息验证
		$scope.saveVerify=function(data){
			if(data.IS_OOS=='1'){
				if(!data.OOS_DATE){
					$.messager.alert("提示","请选择退役日期！","info");
					return false;
				}
				if(!data.OOS_REASON){
					$.messager.alert("提示","请填写退役原因！","info");
					return false;
				}
			}else{
				data.OOS_DATE="";
				data.OOS_REASON="";
			}			
		};
		/*
		 * 获取设备资产记录
		 */
		$scope._getList=function _getList(){
			var queryObj = {};
			queryObj.MN =$scope.stationModel.MN;
			queryObj.STATION_ID =$scope.stationModel.STATION_ID;		
			if(!queryObj.MN && !queryObj.stationId) {
				return;
			}
			if($scope.serveOptionModel!="all"){
				queryObj.IS_OOS=$scope.serveOptionModel;
			}
			$assetsRegisterService.getList(queryObj).then(function(d){
				$scope.assetsRegisterList=d;
			});
		}
	});
}(window, angular));